package com.zpark.cloud.alibaba.controller;

import com.zpark.cloud.alibaba.entity.Order;
import com.zpark.cloud.alibaba.service.IOrderService;
import com.zpark.cloud.alibaba.utils.ResponseResult;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @PackageName: com.zpark.cloud.alibaba.controller
 * @ClassName: OrderController
 * @Description:
 * @author: RZS
 * @date: 2023/6/28  17:16
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Value("${spring.application.name}")
    private String serverName;

    @GetMapping("/add/{uid}/{pid}/{count}/{money}")
    public ResponseResult<Integer> addOrder(@PathVariable("uid") Long uid,
                                            @PathVariable("pid") Long pid,
                                            @PathVariable("count") Integer count,
                                            @PathVariable("money")BigDecimal money){
        // 封装order
        Order order = new Order();
        order.setUserId(uid);
        order.setProductId(pid);
        order.setCount(count);
        order.setMoney(money);

        // 调用业务层
        Integer row = orderService.addOrder(order);

        Integer code = 200;
        String msg = "订单添加成功";

        if (row <= 0) {
            code = 500;
            msg = "添加订单异常";
        }
        return new ResponseResult<>(code, msg, row);
    }
}
